<!--
 * @Author: 弗兰克 1085068573@qq.com
 * @Description: 账号密码组件
-->
<template>
  <div class="login-account" status-icon>
    <el-form label-width="60px" :rules="rules" ref="formRef" :model="account">
      <el-form-item label="账号" prop="name">
        <el-input v-model="account.name" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="account.password" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { rules } from '../config/account-config'
import type { FormInstance } from 'element-plus'
import LocalCache from '@/utils/cache'
import { useStore } from 'vuex'

export default defineComponent({
  setup() {
    const store = useStore()
    const account = reactive({
      name: '',
      password: ''
    })
    const formRef = ref<FormInstance>()
    const loginAction = (isKeepPassword: boolean): void => {
      console.log('formRef===', formRef)
      formRef.value?.validate((valid) => {
        if (valid) {
          //判断是否需要记住密码
          if (isKeepPassword) {
            // 本地缓存
            LocalCache.setCache('userName', account.name)
            LocalCache.setCache('password', account.password)
          } else {
            LocalCache.deleteCache('userName')
            LocalCache.deleteCache('password')
          }

          //开始验证登录
          store.dispatch('loginModule/accountLoginAction', { ...account })
        } else {
          console.log('flase')
        }
      })
    }
    return {
      rules,
      account,
      loginAction,
      formRef
    }
  }
})
</script>

<style lang="less" scoped></style>
